package com.itheima.health.controller.mobile;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderVO;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam){
        String phone = (String) redisTemplate.opsForValue().get("phone");
        String code = (String) redisTemplate.opsForValue().get("code");
        if(phone.equals(orderSubmitParam.getTelephone()) && code.equals(orderSubmitParam.getValidateCode())){
            Order order = orderService.submit(orderSubmitParam);
            Map<String,Integer> orderId = new HashMap<>();
            orderId.put("id", order.getMemberId());
            return new Result(true, MessageConst.ORDER_SUCCESS, orderId);
        }else {
            return new Result(true, MessageConst.VALIDATECODE_ERROR);
        }
    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        OrderVO orderVO = orderService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,orderVO);
    }
}
